package com.hiwhitley.chapter05;

/**
 * Created by hiwhitley on 17-1-14.
 */
public class Replace2 {
    public void replace(char[] chars) {
        if (chars == null || chars.length == 0) {
            return;
        }

        int num = 0;
        int len = 0;
        for (len = 0; len < chars.length && chars[len] != 0; len++) {
            if (chars[len] == ' ') {
                num++;
            }
        }

        int j = len + num * 2;
        for (int i = len - 1; i > -1; i--) {
            if (chars[i] != ' ') {
                chars[j--] = chars[i];
            } else {
                chars[j--] = '0';
                chars[j--] = '2';
                chars[j--] = '%';
            }
        }
    }

    public void modify(char[] chars) {
        if (chars == null || chars.length == 0) {
            return;
        }
        int j = chars.length - 1;
        for (int i = chars.length - 1; i > -1; i--) {
            if (chars[i] != '*') {
                chars[j--] = chars[i];
            }
        }
        while (j > -1) {
            chars[j--] = '*';
        }
    }

    public static void main(String[] args) {
        Replace2 main = new Replace2();
        main.modify(new char[]{'1', '2', '*', '*', '3'});
    }
}
